﻿Imports System.Net.NetworkInformation
Imports setnet.libraries
Imports System.Management

Public Class frmChange
    Dim _SaveFile As New IO.FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\SetNet\profiles.xml")
    Dim _ListofIPConfiguration As New List(Of IPConfiguration)
    Dim _ConfigurationsManager As ConfigurationManager
    Dim WithEvents _CurrencyManager As CurrencyManager
    Dim _EditMode As Boolean = False
    Dim _FrmWait As New frmWait
    Dim _IPConfiguration As IPConfiguration

    Private Sub frmChange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillAdapter()
        ReadSettings()
        ApplyBindings()
        Aktiviere_Gui()
    End Sub

    Private Sub Aktiviere_Gui()
        If lstIPConfiguration.Items.Count = 0 Then
            Me.AcceptButton = btnHinzufügen
        Else
            Me.AcceptButton = btnAktivieren
        End If
        ModifyControls(False)
        If lstIPConfiguration.Items.Count = 0 Then
            btnAktivieren.Enabled = False
            btnÄndern.Enabled = False
            btnLöschen.Enabled = False
        End If
        _CurrencyManager.Refresh()
    End Sub

    Private Sub ModifyControls(ByVal pValue As Boolean)
        For Each tp As TabPage In tcEinstellungen.TabPages
            For Each ctrl As Control In tp.Controls
                ctrl.Enabled = pValue
            Next
        Next
    End Sub

    Private Sub ApplyBindings()
        lstIPConfiguration.DataSource = _ListofIPConfiguration
        lstIPConfiguration.ValueMember = "Displayname"
        txtAnzeigename.DataBindings.Add("Text", _ListofIPConfiguration, "Displayname")
        txtIP.DataBindings.Add("Text", _ListofIPConfiguration, "IPAdresse")
        txtDNS1.DataBindings.Add("Text", _ListofIPConfiguration, "DNS1")
        txtDNS2.DataBindings.Add("Text", _ListofIPConfiguration, "DNS2")
        txtGateway.DataBindings.Add("Text", _ListofIPConfiguration, "Gateway")
        txtProxyIP.DataBindings.Add("Text", _ListofIPConfiguration, "ProxyIP")
        txtProxyPort.DataBindings.Add("Text", _ListofIPConfiguration, "ProxyPort")
        chkDHCP.DataBindings.Add("checked", _ListofIPConfiguration, "DHCP")
        chkProxy.DataBindings.Add("Checked", _ListofIPConfiguration, "Proxy")
        txtProxyAusnahmen.DataBindings.Add("Text", _ListofIPConfiguration, "ProxyExceptions")
        chkProxyUmgehen.DataBindings.Add("Checked", _ListofIPConfiguration, "ProxyByPassLocal")
        txtFFPrefs.DataBindings.Add("Text", _ListofIPConfiguration, "ProxyFFPrefsPath")
        txtScript.DataBindings.Add("Text", _ListofIPConfiguration, "Script")
        txtSubnetz.DataBindings.Add("Text", _ListofIPConfiguration, "Subnetzmaske")
    End Sub

    Public Sub ReadSettings()
        _ConfigurationsManager = New ConfigurationManager
        Try
            _ConfigurationsManager.LoadFromXML(_SaveFile)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        _ListofIPConfiguration = _ConfigurationsManager.getConfigurations
        _CurrencyManager = CType(Me.BindingContext(_ListofIPConfiguration), CurrencyManager)
    End Sub

    Public Sub FillAdapter()
        cmbLanAdapter.Items.Clear()
        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        For Each mo As ManagementObject In moc
            If mo.Item("IPEnabled") = True AndAlso mo.Item("Servicename") <> "NdisIP" Then
                cmbLanAdapter.Items.Add(mo.Item("Description"))
            End If
        Next
    End Sub

    Private Sub SaveSettings()
        CType(lstIPConfiguration.SelectedItem, IPConfiguration).LanAdapter = cmbLanAdapter.SelectedItem
        _ConfigurationsManager.SetList(_ListofIPConfiguration)
        _CurrencyManager.Refresh()
        _ConfigurationsManager.SaveToXML(_SaveFile)
    End Sub

    Private Sub btnHinzufügen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHinzufügen.Click
        If _EditMode Then
            lstIPConfiguration.Enabled = True
            btnHinzufügen.Text = "&Hinzufügen"
            btnLöschen.Enabled = True
            btnÄndern.Enabled = True
            btnHinzufügen.Enabled = True
            btnAktivieren.Enabled = True
            SaveSettings()
            ModifyControls(False)
            _EditMode = False
            Me.AcceptButton = btnAktivieren
        Else
            _ListofIPConfiguration.Add(New IPConfiguration("<Neues Profil>"))
            _CurrencyManager.Refresh()
            _CurrencyManager.Position = _CurrencyManager.Count - 1
            If cmbLanAdapter.Items.Count > 0 Then
                cmbLanAdapter.SelectedIndex = 0
            End If
            lstIPConfiguration.Enabled = False
            btnLöschen.Enabled = False
            btnÄndern.Enabled = False
            btnAktivieren.Enabled = False
            btnHinzufügen.Enabled = True
            btnHinzufügen.Text = "&Speichern"
            _EditMode = True
            ModifyControls(True)
            chkProxy_CheckedChanged(Nothing, Nothing)
            chkDHCP_CheckedChanged(Nothing, Nothing)
            Me.AcceptButton = btnHinzufügen
            txtAnzeigename.Focus()
        End If
    End Sub

    Private Sub btnLöschen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLöschen.Click
        _CurrencyManager.RemoveAt(_CurrencyManager.Position)
        SaveSettings()
        If lstIPConfiguration.Items.Count = 0 Then
            btnAktivieren.Enabled = False
            btnLöschen.Enabled = False
            btnÄndern.Enabled = False
            Me.AcceptButton = btnHinzufügen
        End If
        ModifyControls(False)
    End Sub

    Private Sub chkDHCP_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDHCP.CheckedChanged
        grbNetzwerkkonfiguration.Enabled = Not chkDHCP.Checked And chkDHCP.Enabled
        If chkDHCP.Enabled AndAlso chkDHCP.Checked And _EditMode And _IPConfiguration IsNot Nothing AndAlso DirectCast(lstIPConfiguration.SelectedItem, IPConfiguration).Equals(_IPConfiguration) Then
            _IPConfiguration.DNS1 = ""
            _IPConfiguration.DNS2 = ""
            _IPConfiguration.Gateway = ""
            _IPConfiguration.IPAdresse = ""
            _IPConfiguration.Subnetzmaske = ""
            txtDNS1.Text = ""
            txtDNS2.Text = ""
            txtGateway.Text = ""
            txtIP.Text = ""
            txtSubnetz.Text = ""
        End If
    End Sub

    Private Sub chkProxy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkProxy.CheckedChanged
        grbProxyIE.Enabled = chkProxy.Checked And chkProxy.Enabled
    End Sub

    Public Function IsValidIP(ByVal addr As String) As Boolean
        Dim pattern As String = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\." & _
   "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
        Dim check As New System.Text.RegularExpressions.Regex(pattern)
        Dim valid As Boolean = False
        If addr = "" Then
            valid = True
        Else
            valid = check.IsMatch(addr, 0)
        End If
        Return valid
    End Function

    Private Sub btnAktivieren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAktivieren.Click
        _FrmWait = New frmWait
        _IPConfiguration = CType(lstIPConfiguration.SelectedItem, IPConfiguration)
        Me.Enabled = False
        _FrmWait.Show()
        bwWait.RunWorkerAsync()
    End Sub

    Private Sub btnLookupFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLookupFF.Click
        fbdFFPrefs.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Mozilla\Firefox\Profiles\"
        If fbdFFPrefs.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtFFPrefs.Text = fbdFFPrefs.SelectedPath
        End If
    End Sub

    Private Sub bwWait_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwWait.DoWork
        _IPConfiguration.ApplyConfiguration()
    End Sub

    Private Sub bwWait_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwWait.RunWorkerCompleted
        Me.Enabled = True
        _FrmWait.Close()
    End Sub

    Private Sub btnÄndern_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnÄndern.Click
        If _EditMode Then
            ModifyControls(False)
            lstIPConfiguration.Enabled = True
            btnÄndern.Text = "&Ändern"
            btnLöschen.Enabled = True
            btnHinzufügen.Enabled = True
            btnAktivieren.Enabled = True
            _EditMode = False
            Me.AcceptButton = btnAktivieren
            SaveSettings()
        Else
            ModifyControls(True)
            chkProxy_CheckedChanged(Nothing, Nothing)
            chkDHCP_CheckedChanged(Nothing, Nothing)
            lstIPConfiguration.Enabled = False
            btnLöschen.Enabled = False
            btnHinzufügen.Enabled = False
            btnAktivieren.Enabled = False
            btnÄndern.Text = "&Speichern"
            Me.AcceptButton = btnÄndern
            _EditMode = True
        End If
    End Sub

    Private Sub txtIP_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIP.Leave
        If IsValidIP(txtIP.Text) Then
            txtSubnetz.Text = "255.255.255.0"
            erpFehler.Clear()
        Else
            erpFehler.SetError(txtIP, "Keine gültige IP-Adresse")
            txtIP.Focus()
        End If
    End Sub

    Private Sub txtGateway_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGateway.Leave
        If Not IsValidIP(txtGateway.Text) Then
            erpFehler.SetError(txtGateway, "Keine gültige IP-Adresse")
            txtGateway.Focus()
        Else
            erpFehler.Clear()
        End If
    End Sub

    Private Sub txtDNS1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDNS1.Leave
        If Not IsValidIP(txtDNS1.Text) Then
            erpFehler.SetError(txtDNS1, "Keine gültige IP-Adresse")
            txtDNS1.Focus()
        Else
            erpFehler.Clear()
        End If
    End Sub

    Private Sub txtDNS2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDNS2.Leave
        If Not IsValidIP(txtDNS2.Text) Then
            erpFehler.SetError(txtDNS2, "Keine gültige IP-Adresse")
        Else
            erpFehler.Clear()
        End If
    End Sub


    Private Sub _CurrencyManager_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _CurrencyManager.CurrentChanged
        Try
            _IPConfiguration = CType(lstIPConfiguration.SelectedItem, IPConfiguration)
            cmbLanAdapter.SelectedItem = _IPConfiguration.LanAdapter
        Catch ex As Exception

        End Try

    End Sub


End Class